home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / UXUTIL.C < prev    next >
C/C++ Source or Header  |  1991-10-16  |  6KB  |  244 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/users/jinx/microcode/RCS/uxutil.c,v 1.4 1991/10/16 22:26:19 jinx Exp $
  4.  
  5. Copyright (c) 1990, 1991 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "ux.h"
  36. #include "uxutil.h"
  37. #include <ctype.h>
  38.  
  39. static CONST char *
  40. DEFUN (char_description_brief, (c), unsigned char c)
  41. {
  42.   static char buffer [5];
  43.   switch (c)
  44.     {
  45.     case ' ': return ("SPC");
  46.     case '\t': return ("TAB");
  47.     case '\r': return ("RET");
  48.     case '\n': return ("LFD");
  49.     case '\033': return ("ESC");
  50.     case '\177': return ("DEL");
  51.     default:
  52.       if (c < ' ')
  53.     {
  54.       (buffer[0]) = '^';
  55.       (buffer[1]) = (c + '@');
  56.       (buffer[2]) = '\0';
  57.     }
  58.       else if (c < '\177')
  59.     {
  60.       (buffer[0]) = c;
  61.       (buffer[1]) = '\0';
  62.     }
  63.       else
  64.     {
  65.       (buffer[0]) = '\\';
  66.       (buffer[1]) = (c >> 6);
  67.       (buffer[2]) = ((c >> 3) & 7);
  68.       (buffer[3]) = (c & 7);
  69.       (buffer[4]) = '\0';
  70.     }
  71.       return (buffer);
  72.     }
  73. }
  74.  
  75. CONST char *
  76. DEFUN (char_description, (c, long_p), unsigned char c AND int long_p)
  77. {
  78.   static char buffer [64];
  79.   CONST char * description = (char_description_brief (c));
  80.   if (long_p)
  81.     {
  82.       int meta = (c >= 0200);
  83.       int cc = (c & 0177);
  84.       int control = (cc < 0040);
  85.       if (meta || control)
  86.     {
  87.       sprintf (buffer, "`%s' (%s%s%c)",
  88.            description,
  89.            (meta ? "meta-" : ""),
  90.            (control ? "control-" : ""),
  91.            (control ? (cc + 0100) : cc));
  92.       return (buffer);
  93.     }
  94.     }
  95.   sprintf (buffer, "`%s'", description);
  96.   return (buffer);
  97. }
  98.  
  99. static Ttty_state original_tty_state;
  100.  
  101. void
  102. DEFUN_VOID (UX_initialize_userio)
  103. {
  104.   UX_terminal_get_state (STDIN_FILENO, (&original_tty_state));
  105. }
  106.  
  107. static void
  108. DEFUN (restore_input_state, (ap), PTR ap)
  109. {
  110.   UX_terminal_set_state (STDIN_FILENO, ap);
  111. }
  112.  
  113. static Ttty_state *
  114. DEFUN_VOID (save_input_state)
  115. {
  116.   Ttty_state * s = (dstack_alloc (sizeof (Ttty_state)));
  117.   UX_terminal_get_state (STDIN_FILENO, s);
  118.   transaction_record_action (tat_always, restore_input_state, s);
  119.   return (s);
  120. }
  121.  
  122. void
  123. DEFUN_VOID (userio_buffered_input)
  124. {
  125.   save_input_state ();
  126.   UX_terminal_set_state (STDIN_FILENO, (&original_tty_state));
  127. }
  128.  
  129. char
  130. DEFUN_VOID (userio_read_char)
  131. {
  132.   char c;
  133.   while (1)
  134.     {
  135.       int nread;
  136.  
  137.       errno = 0;
  138.       nread = (UX_read (STDIN_FILENO, (&c), 1));
  139.       if (nread == 1)
  140.     break;
  141.       if (errno != EINTR)
  142.     {
  143.       c = '\0';
  144.       break;
  145.     }
  146.     }
  147.   return (c);
  148. }
  149.  
  150. char
  151. DEFUN_VOID (userio_read_char_raw)
  152. {
  153.   transaction_begin ();
  154.   {
  155.     /* Must split declaration and assignment because some compilers
  156.        do not permit aggregate initializers. */
  157.     Ttty_state state;
  158.     state = (* (save_input_state ()));
  159.     terminal_state_raw ((&state), STDIN_FILENO);
  160.     UX_terminal_set_state (STDIN_FILENO, (&state));
  161.   }
  162.   {
  163.     char c = (userio_read_char ());
  164.     transaction_commit ();
  165.     return (c);
  166.   }
  167. }
  168.  
  169. char
  170. DEFUN (userio_choose_option, (herald, prompt, choices),
  171.        CONST char * herald AND
  172.        CONST char * prompt AND
  173.        CONST char ** choices)
  174. {
  175.   while (1)
  176.     {
  177.       fputs (herald, stdout);
  178.       putc ('\n', stdout);
  179.       {
  180.     CONST char ** scan = choices;
  181.     while (1)
  182.       {
  183.         CONST char * choice = (*scan++);
  184.         if (choice == 0)
  185.           break;
  186.         fprintf (stdout, "  %s\n", choice);
  187.       }
  188.       }
  189.       fputs (prompt, stdout);
  190.       fflush (stdout);
  191.       {
  192.     char command = (userio_read_char_raw ());
  193.     if ((command == '\0') && (errno != 0))
  194.       return (command);
  195.     putc ('\n', stdout);
  196.     fflush (stdout);
  197.     if (islower (command))
  198.       command = (toupper (command));
  199.     {
  200.       CONST char ** scan = choices;
  201.       while (1)
  202.         {
  203.           CONST char * choice = (*scan++);
  204.           if (choice == 0)
  205.         break;
  206.           {
  207.         char option = (*choice);
  208.         if (islower (option))
  209.           option = (toupper (option));
  210.         if (command == option)
  211.           return (option);
  212.           }
  213.         }
  214.     }
  215.       }
  216.     }
  217. }
  218.  
  219. int
  220. DEFUN (userio_confirm, (prompt), CONST char * prompt)
  221. {
  222.   while (1)
  223.     {
  224.       fputs (prompt, stdout);
  225.       fflush (stdout);
  226.       switch (userio_read_char_raw ())
  227.     {
  228.     case 'y':
  229.     case 'Y':
  230.       return (1);
  231.     case 'n':
  232.     case 'N':
  233.       return (0);
  234.     case '\0':
  235.       if (errno != 0)
  236.       {
  237.         /* IO problems, assume everything scrod. */
  238.         fprintf (stderr, "Problems reading keyboard input -- exiting.\n");
  239.         termination_eof ();
  240.       }
  241.     }
  242.     }
  243. }
  244.